package com.xbongbong.paas.model.impl;

import com.xbongbong.paas.domain.dao.PaasProcessNodeTaskDao;
import com.xbongbong.paas.domain.entity.PaasProcessNodeTaskEntity;
import com.xbongbong.paas.domain.entity.ext.PaasProcessNodeTaskEntityExt;
import com.xbongbong.paas.model.PaasProcessNodeTaskModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.StringConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: wufeng
 * @date: 2018/8/13 15:22
 * @desrcption: PaaS流程任务实现类
 */
@Service("paasProcessNodeTaskModel")
public class PaasProcessNodeTaskModelImpl implements PaasProcessNodeTaskModel {

    private static final String COLUMNS = " id, corpid, app_id, menu_id, form_id, task_id, user_id, task_type, node_type,sign_type, last_node_id, template_node_id,opinion,images,attachment,transfer_user_id,revoke_user_id, backspace_node, activation_node, creator_id, add_time, update_time, del, is_read, dingtalk_workrecord_activity_id, dingtalk_workrecord_task_id ";
    /**
     * mysql 5.7 默认开启 only_full_group_by 修改列名
     */
    private static final String GROUP_COLUMNS = " any_value(id) id, any_value(corpid) corpid, any_value(app_id) app_id, any_value(menu_id) menu_id, any_value(form_id) form_id, task_id, any_value(user_id) user_id, any_value(task_type) task_type, any_value(node_type) node_type, any_value(sign_type) sign_type, any_value(last_node_id) last_node_id, any_value(template_node_id) template_node_id, any_value(opinion) opinion, any_value(images) images, any_value(attachment) attachment, any_value(transfer_user_id) transfer_user_id, any_value(revoke_user_id) revoke_user_id, any_value(backspace_node) backspace_node, any_value(activation_node) activation_node, any_value(creator_id) creator_id, any_value(add_time) add_time, any_value(update_time) update_time, any_value(del) del, any_value(is_read) is_read ";


    @Resource
    private PaasProcessNodeTaskDao processNodeTaskDao;

    @Override
    public Integer insert(PaasProcessNodeTaskEntity entity) {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return processNodeTaskDao.insert(entity);
    }

    @Override
    public void insertBatch(List<PaasProcessNodeTaskEntity> list) {
        processNodeTaskDao.insertBatch(list);
    }

    @Override
    public void updateBatch(List<PaasProcessNodeTaskEntity> list, String corpid) {
        processNodeTaskDao.updateBatch(list, corpid);
    }

    @Override
    public List<PaasProcessNodeTaskEntityExt> findEntitys(Map<String, Object> map) {
        // TODO 需优化
        if (!map.containsKey(StringConstant.COLUMNS)) {
            map.put("columns", new StringBuffer(COLUMNS));
        }
        return processNodeTaskDao.findEntitys(map);
    }

    @Override
    public PaasProcessNodeTaskEntity getByKey(Integer key, String corpid) {
        return processNodeTaskDao.getByKey(key, corpid);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> map) {
        return processNodeTaskDao.getEntitysCount(map);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer delete(PaasProcessNodeTaskEntity entity) {
        return processNodeTaskDao.deleteByKey(entity.getId().intValue(), entity.getCorpid());
    }

    @Override
    public Integer update(PaasProcessNodeTaskEntity entity) {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        return processNodeTaskDao.update(entity);
    }

    @Override
    public PaasProcessNodeTaskEntityExt newProcessTask(Map<String, Object> map) {
        // TODO 需优化
        map.put("columns", new StringBuffer(COLUMNS)
                .append(", (select name from tb_user where user_id = task.user_id) as user_name")
                .append(", (select name from tb_paas_process_template_node where id = task.template_node_id) as node_name ")
                .append(", (select summary from tb_paas_process_template_node where id = task.template_node_id) as summary"));
        return processNodeTaskDao.newProcessTask(map);
    }

    @Override
    public List<PaasProcessNodeTaskEntityExt> getLatestNodeForList(Map<String, Object> map) {
        map.put("columns", COLUMNS);
        map.put("groupColumns", GROUP_COLUMNS);
        return processNodeTaskDao.getLatestNodeForList(map);
    }

    @Override
    public void deleteByTaskId(Long taskId, String corpid) {
        processNodeTaskDao.deleteByTaskId(taskId, corpid);
    }

    @Override
    public void deleteBatch(List<PaasProcessNodeTaskEntityExt> taskList, String corpid) {
        processNodeTaskDao.deleteBatch(taskList, corpid);
    }

    @Override
    public Integer getTaskCount(Map<String, Object> entity) {
        return processNodeTaskDao.getTaskCount(entity);
    }

    @Override
    public Integer readStartNode(PaasProcessNodeTaskEntity entity) throws XbbException {
        // 不更新开始节点的更新时间
        return processNodeTaskDao.update(entity);
    }

    /**
     * 查询钉钉实例的任务
     * @param corpid
     * @param taskId
     * @return
     */
    @Override
    public List<String> getDingtalkProcessActivityId(String corpid, Integer taskId) {
        return processNodeTaskDao.getDingtalkProcessActivityId(corpid, taskId);
    }

    @Override
    public List<PaasProcessNodeTaskEntityExt> getJoinNodeForList(Map<String, Object> map) {
        return processNodeTaskDao.getJoinNodeForList(map);
    }

    @Override
    public Integer getJoinNodeCount(Map<String, Object> map) {
        return processNodeTaskDao.getJoinNodeCount(map);
    }

    @Override
    public Integer deleteByFormId(Long formId, String corpid) {
        return processNodeTaskDao.deleteByFormId(formId, corpid);
    }

    @Override
    public void readAllCcList(HashMap<String, Object> param) {
         processNodeTaskDao.readAllCcList(param);
    }

    @Override
    public Integer getTaskCountLimit(Map<String, Object> entity) {
        return processNodeTaskDao.getTaskCountLimit(entity).size();
    }
}
